<?php
namespace Act\Os;
use Lib\Action;
class Backend extends Action{
	const PAGE_SIZE = 25;
	public function __construct($server,$request_method){
		parent::__construct($server,$request_method);
		if($request_method=='get' && !is_ajax()){
			referer($_SERVER['REQUEST_URI']);
		}
		$admin_mod = model('#User','adm_');
		$exclude = array('/os/login','/os/logOut');
		if(!in_array($this->act_name, $exclude) && !$admin_mod->isLogin()){
			$this->assign(array(
				'adm_template'=>$admin_mod->getTemplate(),
			));
			return redirect('/os/login');
		}

		$menu = model('#MenuUrl','adm_')->getMenuByUrl($this->act_name);
		if(empty($menu) && !in_array($this->act_name, $exclude)){
			throw new \Exception("该菜单未加入权限表", 1);
		}
		$this->init($menu,$admin_mod);
		if(in_array($this->act_name, $exclude)){
			return;
		}
		
		if(session('uid')!=1 && !$admin_mod->checkActionAccess()){
			throw new \Exception("您当前没有权限访问该功能，如需访问请联系管理员开通权限", 1);
		}
	}

	public function sysLog($user_name, $type, $model ,$id, $data=''){
		return model('#SysLog','adm_')->addLog ($user_name, $type, $model ,$id, $data);
	}

	public function init($menu,$admin_mod){
		if(!is_ajax()){
			$this->assign(array(
				'adm_templates' => array(
					'default'=>"默认模板",
					'schoolpainting'=>'青葱校园',
					'blacktie'=>'黑色领结',
					'wintertide'=>'冰雪冬季',
				),
				'user_info'=>session('user_info'),
				'sidebarStatus'=>cookie('sidebarStatus') ? cookie('sidebarStatus') : 'yes',
				'adm_template'=>$admin_mod->getTemplate(),
				'sidebar'=>$admin_mod->getSidebar(),
				'content_header'=>$menu,
				'current_module_id'=>isset($menu['module_id']) ? $menu['module_id'] : 0,
				'page_title'=>isset($menu['menu_name']) ? $menu['menu_name'] : '',
				'admin_quick_note' => session('user_info')['show_quicknote'] ? $admin_mod->getQuickNote() : ''
			));
		}
	}

	public function alert($type,$message=""){
		if(empty($message)) {
			switch(strtolower($type)){
				case "success":
					$message=\Lib\ErrorMessage::SUCCESS;
					break;
				case "error" :
					$message=\Lib\ErrorMessage::ERROR;
					break;
			}
		}
		$alert_html="<div class=\"alert alert-$type\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button>$message</div>";
		$this->assign("admin_action_alert",$alert_html);
	}


	public function message($message_detail, $forward_url='', $second = 3,$type="message") {
		switch ($type) {
			case "success" :
				$page_title="操作成功！";
				break;
			case "error":
				$page_title="错误!";
				break;
			default:
				$page_title="嗯!";
				break;
		}
		if(empty($forward_url)){
			$forward_url = str_replace(site_url(),'',isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : site_url().'/os/index');
		}
		$temp = explode('?',$forward_url);
		$file_url = $temp[0];
		if($file_url{0} !=="/"){
			$file_url ='/'.$file_url;
			$forward_url ='/'.$forward_url;
		}
		$menu = model('#MenuUrl','adm_')->getMenuByUrl($file_url);
		$forward_title = "首页";
		if(sizeof($menu)>0){
			$forward_title = $menu['menu_name'];
		}
		if ($forward_url) {
			$message_detail = "$message_detail <script>setTimeout(\"window.location.href ='".(strpos($forward_url,'http')===0 ? site_url() : '')."$forward_url';\", " . ($second * 1000) . ");</script>";
		}
		$this->assign(array(
			'type'=>$type,
			'page_title'=>$page_title,
			'message_detail'=>$message_detail,
			'forward_url'=>$forward_url ,
			'forward_title'=>$forward_title,
		));
		$this->display('public:message.php');
	}

	public function error($message_detail, $forward_url='', $second = 3,$type="error") {
		return $this->message($message_detail, $forward_url, $second ,$type);
	}

	public function success($message_detail, $forward_url='', $second = 3 ,$type="success") {
		return $this->message($message_detail, $forward_url, $second, $type);
	}
}